﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Rbac.Contracts.DTO.InPut;
using Rbac.Contracts.DTO.OutPut;
using Rbac.Contracts.Interface;

namespace Rbac.API.Controllers
{
	/// <summary>
	/// 用户管理
	/// </summary>
	[Route("api/[controller]/[action]")]
	[ApiController]
	//[HiddenApi]
	public class UserController : ControllerBase
	{
		private readonly IUserServer _userServer;
		private readonly TokenService _tokenService;
		private readonly ILogger<UserController> _logger;
		public UserController(IUserServer userServer, TokenService tokenService, ILogger<UserController> logger)
		{
			_userServer = userServer;
			_tokenService = tokenService;
			_logger = logger;
		}
		/// <summary>
		/// 获取查询分页
		/// </summary>
		/// <remarks>用户查询的详细说明</remarks>
		/// <returns></returns>
		[HttpPost]//请求谓词
		[Authorize]
		public Results<PageOutPut<UserOutPut>> GetUsers([FromBody] GetUsersInput input)
		{
			return Results<PageOutPut<UserOutPut>>.DataResult(_userServer.GetUserOuts(input));
		}
		///// <summary>
		///// 用户查询2
		///// </summary>
		///// <remarks>用户查询的详细说明</remarks>
		///// <returns></returns>
		//[HttpGet]//请求谓词
		//[HiddenApi]//隐藏接口
		//[NonAction]//不调用
		//public List<UserOutPut> GetUsers2()
		//{
		//	return _userServer.GetUserOuts();
		//}
		/// <summary>
		/// 登录
		/// </summary>
		/// <returns></returns>
		[HttpPost]
		public Results<UserOutPut> Login([FromBody]LoginInPut input)
		{
			var userDto= _userServer.Login(input);
			if (userDto != null)
			{
				_logger.LogInformation(userDto.UserName+"登陆了");
				var token = _tokenService.GenerateToken(userDto);
				return Results<UserOutPut>.TokenResult(token,userDto);
			}
			_logger.LogError("账号"+ input.UserName+"失败了");
			return Results<UserOutPut>.FailResult("用户名密码错误");
		}
	}
}
